package cn.onlineoedering.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormat {
	private static final SimpleDateFormat FORMATE_OTHER_YEAR = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static final SimpleDateFormat FORMATE_THIS_YEAR = new SimpleDateFormat("M月d日 HH:mm");
	private static final SimpleDateFormat FORMATE_TODAY = new SimpleDateFormat("今天 H:mm");

	public static String getFormattedDate(Date date) {

		Calendar current = Calendar.getInstance();// 当前时间
		Calendar paramaterTime = Calendar.getInstance();
		paramaterTime.setTime(date);// 参数时间

		long currentLong = current.getTimeInMillis();
		long paramterTimeLong = paramaterTime.getTimeInMillis();
		long time = (currentLong - paramterTimeLong) / 1000;// 时间差，秒数
		long second = time / 10 * 10;
		long minute = time / 60;

		current.set(current.get(Calendar.YEAR), current.get(Calendar.MONTH), current.get(Calendar.DATE), 0, 0, 0);
		long todayZeroLong = current.getTimeInMillis();
		long sinceTodayZeroLong = (currentLong - todayZeroLong) / 1000;

		if (time < 0) {
			return "穿越了...";
		} else if (time < 10) {
			return "10秒前";
		} else if (time < 60) {
			return second + "秒前";
		} else if (time < 60 * 60) {
			return minute + "分钟前";
		} else if (time < sinceTodayZeroLong) {
			return FORMATE_TODAY.format(date);
		} else if (paramaterTime.get(Calendar.YEAR) == current.get(Calendar.YEAR)) {
			return FORMATE_THIS_YEAR.format(date);
		} else {
			return FORMATE_OTHER_YEAR.format(date);
		}
	}

	public static String getFormattedDate(long rawtime) {
		return getFormattedDate(new Date(rawtime));
	}

	public static void main(String[] args) {

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = simpleDateFormat.parse("2012-11-8 16:42:15");
			System.out.println(getFormattedDate(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
